<?php
class MemResust {
	
	public $columns = array();
	public $columns_update = array();
	public $return;
	public $pointer = -1;
	public $total;
	
	function __construct($sql, $link, $sgbd){
		switch ($sgbd){
			case MYSQL: $this->mysql_select($sql, $link); break;
			case MSSQL: $this->mssql_select($sql, $link); break;
		}
	}
	
	function __get($column){
		if($this->pointer == -1)
			$this->pointer = 0;
		if(array_key_exists($column, $this->return[$this->pointer]))
	       	return $this->return[$this->pointer][$column];
	    else
	    	return '';
	}
	
	function mysql_select($sql, $link){
		$aux = -1;
		if($rs = mysql_query($sql, $link)){
			$aux = mysql_num_rows($rs);
		}
		if($aux == -1){
		   $error =  array (
		    	"message"  => $this->erro_select,
				"database_reports" => mysql_error() 
		    );
		    throw new MemDbException($error);
		}else{
			$i = 0;
			$this->total = mysql_num_rows($rs);
			
			while($line = mysql_fetch_array($rs)){
				$this->columns = array_keys($line);
				for($k=0;count($this->columns) > $k; $k++){
					$this->return[$i][$this->columns[$k]] = $line[$this->columns[$k]];
				}
				$i++;
			}
		}
	}
	
	function mssql_select($sql, $link){
		
	}
	
	function next(){
		$this->pointer = $this->pointer + 1;
		if($this->total > $this->pointer){
			return true;
		}else{
			$this->pointer = $this->pointer - 1;
			return false;
		}
	}
	
	function prev(){
		$this->pointer = $this->pointer - 1;
		if(0 <= $this->pointer){
			return true;
		}else{
			$this->pointer = $this->pointer + 1;
			return false;
		}	
	}
	function encode(){
		for($r=0;$this->return[$r];$r++){
			for($c=0;$c<=count($this->columns);$c++){
				$this->return[$r][$this->columns[$c]] = utf8_encode($this->return[$r][$this->columns[$c]]); 
			}
		}
	}
}